package com.yh.blog.controller.admin;

import com.yh.blog.domain.Blog;
import com.yh.blog.domain.Users;
import com.yh.blog.service.BlogService;
import com.yh.blog.service.TypesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 博客管理控制层
 */
@Controller
@RequestMapping("/admin")
public class BlogController {

    @Resource
    private BlogService blogService;
    @Autowired
    private TypesService typesService;

//    博客列表
    @RequestMapping("/blog")
    public ModelAndView list(@RequestParam(value = "page",defaultValue = "1") Integer page,Blog condition) {
        return new ModelAndView("admin/blog/blogs")
                .addObject("types",this.typesService.list())
                .addObject("pager",this.blogService.list(condition,page))
                .addObject("condition",condition);
    }

//    跳转到新增页面
    @GetMapping("/blog/input")
    public String goappend(Model model){
        model.addAttribute("blog",new Blog());
        model.addAttribute("types",this.typesService.list());
        return "admin/blog/blogs-input";
    }
//    新增博客
    @PostMapping("/blogs")
    public String append(Blog blog, RedirectAttributes attributes, HttpSession session){
        blog.setUser((Users) session.getAttribute("user"));
        blog.setType(this.typesService.detail(blog.getType().getTypesId()));
        blog.setTypesId(blog.getType().getTypesId());
        blog.setUserId(blog.getUser().getUserId());
        if(this.blogService.append(blog)){
            attributes.addFlashAttribute("message","新增成功！");
        }else{
            attributes.addFlashAttribute("message","操作失败！");
        }
        return "redirect:/admin/blog";
    }

//  跳转到修改页面
    @GetMapping("/blog/modify")
    public String gomodify(Integer blogId,Model model){
        model.addAttribute("types",this.typesService.list());
        model.addAttribute("blog",this.blogService.detail(blogId));
        return "admin/blog/blogs-modify";
    }

//  修改博客
    @PostMapping("/blogs/modify")
    public String modify(Blog blog, RedirectAttributes attributes){
        if(this.blogService.modify(blog)){
            attributes.addFlashAttribute("message","修改成功！");
        }else{
            attributes.addFlashAttribute("message","操作失败！");
        }
        return "redirect:/admin/blog";
    }

//    删除博客
    @GetMapping("/blog/delete")
    public String delete(@RequestParam("blogId") Integer blodId,RedirectAttributes attributes){
        if(this.blogService.remove(blodId)){
            attributes.addFlashAttribute("message","删除成功！");
        }else{
            attributes.addFlashAttribute("message","操作失败！");
        }
        return "redirect:/admin/blog";
    }

}
